package com.bsj.pay.service.impl;

import com.bsj.pay.dao.PayGoodsDao;
import com.bsj.pay.model.PayGoodsBean;
import com.bsj.pay.service.PayGoodsService;
import com.bsj.pay.service.PayPoolService;
import com.bsj.pay.util.ThreadPoolUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class PayGoodsServiceImpl extends BaseServiceImpl<PayGoodsDao, PayGoodsBean> implements PayGoodsService {

    @Autowired
    private PayPoolService payPoolService;

    @Override
    public void UpdatePayPoolTask() throws Exception {

        List<PayGoodsBean> generateList = this.dao.getNeedGenerateList();
        if (generateList == null || generateList.size() == 0) return;
        for (PayGoodsBean payGoods : generateList) {
            ThreadPoolUtils.getQueryOrderStatePool().execute(() -> {
                logger.info("开启预产池线程,预产池ID:{}", payGoods.getId());
                if (payGoods.getCurrent_count() < payGoods.getPay_url_count()) {
                    payPoolService.generatePayUrl(payGoods);
                }
                logger.info("预产池线程已结束！");
            });
        }
        do {
            Thread.sleep(1000);
        } while (ThreadPoolUtils.getPayPool().getActiveCount() != 0);
    }

    @Override
    public void removeExpirePayPoolTask() throws Exception {
        payPoolService.removeExpired();
    }
}
